<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <todo-list>
			<todo-item @delete="handleDelete" v-for="item in list" :title="item.title" :del="item.del">
				<!-- 传入具名插槽 -->
				<!-- 也是作用域插槽， 子组件传值给父组件 -->
				<template v-slot:pre-icon="{v1, v2}">
					<span>前置 {{v1 + " - " + v2}}</span>
				</template>
				<!-- 不传后置插槽，走默认值 -->
			</todo-item>
		</todo-list>
    </div>
    <script src="./script/vue.js"></script>
    <script>
        Vue.component('todo-item', {
            props: {
                title: String,
                del: {
                    type: Boolean,
                    default: false,
                },
            },
            template: `
            <li>
				<!-- 将值传到外面 -->
				<slot name="pre-icon" :v1="value1" :v2="value2"></slot>
                <span v-if="!del">{{title}}</span>
                <span v-else style="text-decoration: line-through">{{title}}</span>
                <button @click="handleClick" v-show="!del">删除</button>
				<!-- 设置插槽默认值为“😄” -->
				<slot name="suf-icon">😄</slot>
            </li>
          `,
            data: function() {
                return {
					value1: Math.random(),
                    value2: Math.random(),
                }
            },
            methods: {
				handleClick : function() {
					console.log('click del!');
					<!-- 触发自定义事件 -->
                    this.$emit('delete', this.title)
				}
            },
        })
        Vue.component('todo-list', {
            template: `
            <ul>
              <slot></slot>
            </ul>
          `,
            data: function() {
                return {
                }
            },
        })
        var vm = new Vue({
            el: '#app',
            data: {
				list: [{
					title: '课程1',
					del: false
				}, {
					title: '课程2',
					del: true
				}],
			},
			methods: {
				handleDelete : function(title) {
					console.log('title = ' + title);
				}
			},
        })
    </script>
</body>

</html>